package com.ml.mall.security;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.ml.mall.entity.sys.SysUser;
import com.ml.mall.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.UUID;

/**
 * @description:
 * @author kyyang
 * @date 2020/12/28 20:16
 * @version 1.0
 */
@Slf4j
public class JwtUtil {

    /**
     * 校验token是否正确
     *
     * @param token    密钥
     * @param password 用户的密码
     * @return 是否正确
     */
    public static boolean verify(String token, String username, String password) {
        JWTVerifier verifier = null;
        try {
            Algorithm algorithm = Algorithm.HMAC256(password);
            verifier = JWT.require(algorithm).withClaim("username", username).build();
            DecodedJWT jwt = verifier.verify(token);
            log.info("jwt[{}]",jwt);
        } catch (Exception e) {
            return false;
        }

        return true;

    }

    /**
     * 获得token中的信息无需secret解密也能获得
     *
     * @return token中包含的用户名
     */
    public static String getUsername(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("username").asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    public static Long getUserId() {

            return getUserId(HttpUtil.getToken());
    }

    public static SysUser getCurrentUser(){
        return ShiroFactroy.me().user(getUserId());
    }

    public static SysUser getUserByToken(String token){
        return ShiroFactroy.me().user(getUserId(token));
    }



    public static Long getUserId(String token) {
        try {
            //todo：test
            if(token==null){
               //管理员
                //return 1L;
               //合作商
//                return 2L;
               //卖家
                //return 3L;
               //买家
                return 138L;
            }
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("userId").asLong();
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    /**
     * 生成签名
     *
     * @param user       用户
     * @param expireTime 毫秒数
     * @return 加密的token
     */
    public static String sign(SysUser user, long expireTime) {
        try {
            Date date = new Date(System.currentTimeMillis() + expireTime);
            Algorithm algorithm = Algorithm.HMAC256(user.getPwd());
            // 附带username信息
            return JWT.create()
                    .withClaim("username", user.getAcc())
                    .withClaim("userId", user.getId())
                    .withClaim("uuid", UUID.randomUUID().toString())
                    .withExpiresAt(date)
                    .sign(algorithm);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }
}
